
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        session_start();

        define('DS', DIRECTORY_SEPARATOR);
        define('HOME', dirname(__FILE__));
        ini_set('display_errors', 1);

        require_once HOME . DS . 'config.php';
        require_once HOME . DS . 'application' . DS . 'controllers' . DS . 'controller.php';
        require_once HOME . DS . 'application' . DS . 'models' . DS . 'model.php';
        require_once HOME . DS . 'resources' . DS . 'facebook' . DS . 'facebook.php';

        function __autoload($class) {
            if (file_exists(HOME . DS . 'resources' . DS . strtolower($class) . '.php')) {
                require_once HOME . DS . 'resources' . DS . strtolower($class) . '.php';
            } else if (file_exists(HOME . DS . 'resources' . DS . 'facebook' . DS . strtolower($class) . '.php')) {
                require_once HOME . DS . 'resources' . DS . 'facebook' . DS . strtolower($class) . '.php';
            } else if (file_exists(HOME . DS . 'application' . DS . 'models' . DS . strtolower(strtolower($class)) . '.php')) {
                require_once HOME . DS . 'application' . DS . 'models' . DS . strtolower($class) . '.php';
            } else if (file_exists(HOME . DS . 'application' . DS . 'controllers' . DS . strtolower($class) . '.php')) {
                require_once HOME . DS . 'application' . DS . 'controllers' . DS . strtolower($class) . '.php';
            }
        }
        
            
             
        $loginState = "fb_" . FACEBOOK_APP_ID . "_code";
        $url = $_SERVER["REQUEST_URI"];
        
        // echo $url;
        
        
        if (!empty($_SESSION[$loginState]) and isset($_SESSION[$loginState])) {
            $username_id = $_SESSION["fb_id"];
            $linkProfile = "http://www.facebook.com/profile.php?id=" . $username_id;
            $display_name = $_SESSION["fb_display_name"];
            $sns = "facebook";
            
           
            // Store to people Model
            $peopleModel = new peopleModel();

            // Check if exist people in Database
            if ($peopleModel->getPeoplebByUsername($username_id)) {
                $peopleModel->updateDisplayNameByUsernameId($username_id, $display_name);
            } else {
                
                // echo "Store new people". "<br/>";
                
                // $peopleModel->setIdPeople($id);
                $peopleModel->setUsername($username_id);
                $peopleModel->setLinkProfile($linkProfile);
                $peopleModel->setSns($sns);
                $peopleModel->setDisplayName($display_name);

                $peopleModel->storePeopleToDB() or die("Error when store People to DB");
            }
            require_once HOME . DS . 'resources' . DS . 'bootstrap.php';
        } else {
            echo "Some thing";
            $facebook = new Facebook(array(
                'appId' => FACEBOOK_APP_ID,
                'secret' => FACEBOOK_APP_SECRET,
                'cookie' => true
            ));
            
            $user = $facebook->getUser();
            $user_profile = '';

            if ($user) {
                try {
                    // Proceed knowing you have a logged in user who's authenticated.
                    $user_profile = $facebook->api('/me');
                    $_SESSION["fb_id"] = $user_profile["id"];
                    $_SESSION["fb_display_name"] = $user_profile["name"];
                    $_SESSION["fb_username"] = $user_profile["username"];
                } catch (FacebookApiException $e) {
                    error_log($e);
                    $user = null;
                }
                
            } else {

                $loginUrl = $facebook->getLoginUrl(array(
                    'scope' => 'offline_access, publish_actions, user_birthday',
                    "redirect_uri" => "http://fresher2013.live"
                ));

                echo("<script> top.location.href='" . $loginUrl . "'</script>");
            }
            require_once HOME . DS . 'resources' . DS . 'bootstrap.php';
        }
          
   
        ?>
    </body>
</html>

